﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Model;
using Model.Enum;
using UIDesign;
using MsDAL;
using Components;

namespace UIMarker
{
    public partial class FrmMain
    {
        Sys5a00DAL sys5a00Dal = new Sys5a00DAL();
        Sys0a60DAL sys0a60Dal = new Sys0a60DAL(); //编码规则对象
        Sys5a01DAL sys5a01Dal = new Sys5a01DAL();
        Sys5a02DAL sys5a02Dal = new Sys5a02DAL();
        CommonDAL dal = new CommonDAL();

        //保存
        private void tsbtn_Save_Click(object sender, EventArgs e)
        {
            List<SqlApp> lst = new List<SqlApp>();
            string pageName = txtPageName.Text;
            string ctls = ctlname.Text;
            if (string.IsNullOrEmpty(pageName))
            {
                MessageBox.Show("请输入页面名称！", "提示");
                txtPageName.Focus();
                return;
            }
            else if (string.IsNullOrEmpty(ctls))
            {
                MessageBox.Show("请选择控件！", "提示");
                ctlname.Focus();
                return;
            }
            else if (this.flwMain.Controls.Count == 0)
            {
                MessageBox.Show("这个页面不能没有字段啊！", "提示");
                flwMain.Focus();
                return;
            }

            ItemLabel item = null;
            Sys5a00 sys5a00 = null;
            Sys5a01 sys5a01 = null;
            int count = 0;

            if ("A" == this.action) //新增
            {
                //检验页面控件是否已经存在
                if (sys5a00Dal.IsExists(pageName, ctls))
                {
                    MessageBox.Show("页面 + 控件已经存在！", "提示");
                    txtPageName.Focus();
                    return;
                }

                sys5a00 = new Sys5a00();
                sys5a00.Ctlname = this.ctlname.Text;
                sys5a00.Listid = sys0a60Dal.CreateSn("sys5a00_listid", "zqw"); //生成编码规则
                sys5a00.Lsttime = DateTime.Now;
                sys5a00.Pagename = this.txtPageName.Text;
                sys5a00.Pagetitle = this.pagetitle.Text;
                sys5a00.Remark = this.remark.Text;
                sys5a00.Colsize = Validation.ToInt(txtcolsize.Text);
                lst.Add(sys5a00Dal.Add(sys5a00));
            }

            foreach (Control c in this.flwMain.Controls)
            {
                item = c as ItemLabel;
                sys5a01 = new Sys5a01();
                if (null == item) continue;

                CommonUtil.GetInstance.CopyProperties(item, sys5a01, true, true);
                if (string.IsNullOrEmpty(item.Listid))
                {
                    sys5a01.Orderid = count;
                    sys5a01.Plistid = string.IsNullOrEmpty(this.p_listid) ? sys5a00.Listid : this.p_listid;
                    sys5a01.Listid = sys0a60Dal.CreateSn("sys5a01_listid", "zqw"); //调用编码规则生成主键 
                    lst.Add(sys5a01Dal.Add(sys5a01));
                }
                else
                {
                    item.Orderid = count;
                    lst.Add(sys5a01Dal.Update(sys5a01));
                }

                count++;
            }

            Sys5a02 sys5a02 = null;
            object mk = null; //修改标识
            foreach (DataGridViewRow drSource in this.dgvSource.Rows)
            {
                mk = drSource.Cells["mk"].Value;
                sys5a02 = drSource.Cells["listid"].Tag as Sys5a02;
                if (sys5a02 == null) continue;
                if (Validation.IsNull(mk)) //新增
                {
                    sys5a02.Listid = sys0a60Dal.CreateSn("sys5a02_listid", "zqw"); //调用编码规则生成主键 
                    sys5a02.Plistid = ("A" == this.action) ? sys5a00.Listid : p_listid;
                    lst.Add(sys5a02Dal.Add(sys5a02));
                }
                else //修改
                {
                    sys5a02.Plistid = this.p_listid;
                    lst.Add(sys5a02Dal.Update(sys5a02));
                }
            }

            //删除项
            if (null != delItems)
            {
                foreach (Sys5a01 s in delItems)
                {
                    lst.Add(sys5a01Dal.Delete(s.Listid));
                }
            }

            //删除数据源
            if (null != delAction)
            {
                foreach (string lid in delAction) lst.Add(sys5a02Dal.Delete(lid));
            }

            string error = dal.SaveData(lst);
            MessageBox.Show(string.IsNullOrEmpty(error) ? "OK～" : error);

            if (string.IsNullOrEmpty(error) && string.IsNullOrEmpty(this.p_listid)) //重置
            {
                tsBtn_Reset_Click(null, null);
                this.dgvSource.Rows.Clear();
                this.txtcolsize.Clear();
                this.txtcolsize.Enabled = false;
                this.txtPageName.Clear();
                this.txtPageName.Enabled = false;
                this.action = "V";
            }
            else if (String.IsNullOrEmpty(error))
            {
                tsBtn_Reset_Click(null, null);
            }
        }
    }
}
